import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import userLayout from '../components/user-layout'
import userLayout2 from '../components/user-layout2'
import store from '../store'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.repalce = function replace (location) {
  return originalReplace.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/my',
},
{
  path: '/my',
  component: userLayout,
  children: [{
    path: '',
    component: () =>
      import( /* webpackChunkName: "my" */ '../views/my'),
    meta: {
      requireLogin: true,
      title: '个人资料',
      footer: 2
    }
  },
  {
    path: 'config',
    component: () =>
      import( /* webpackChunkName: "my" */ '../views/config'),
    meta: {
      requireLogin: true,
      title: '个人资料',
      footer: 2,
      nav_back: true
    }
  },
  {
    path: 'password/reset',
    component: () =>
      import( /* webpackChunkName: "my" */ '../views/config/password-reset'),
    meta: {
      requireLogin: true,
      title: '修改密码',
      footer: 2,
      nav_back: true
    }
  },
  {
    path: 'bind',
    component: () =>
      import( /* webpackChunkName: "my" */ '../views/config/bind'),
    meta: {
      requireLogin: true,
      title: '信息绑定',
      footer: 'hidden',
    }
  },
  {
    path: 'mobile/bind',
    component: () =>
      import( /* webpackChunkName: "my" */ '../views/config/mobile-bind'),
    meta: {
      requireLogin: true,
      title: '手机号',
      footer: 2,
      nav_back: true
    }
  },
  {
    path: 'bankcards',
    component: () =>
      import( /* webpackChunkName: "my" */ '../views/config/bankcards'),
    meta: {
      requireLogin: true,
      title: '银行卡',
      footer: 2,
      nav_back: true
    }
  },
  {
    path: 'bankcard/bind',
    component: () =>
      import( /* webpackChunkName: "my" */ '../views/config/bankcard-bind'),
    meta: {
      requireLogin: true,
      title: '银行卡',
      footer: 2,
      nav_back: true
    }
  },

  ],

},
{
  path: '/my/buys',
  component: () =>
    import( /* webpackChunkName: "my" */ '../views/my-buy'),
  meta: {
    requireLogin: true,
    footer: 2,
    title: '买入明细',
  }
},
{
  path: '/my/sells',
  component: () =>
    import( /* webpackChunkName: "my" */ '../views/my-sell'),
  meta: {
    requireLogin: true,
    footer: 2,
    title: '卖出明细',
  }
},
{
  path: '/business',
  component: userLayout2,
  children: [{
    path: 'buy',
    component: () =>
      import( /* webpackChunkName: "my" */ '../views/buy'),
    meta: {
      requireLogin: true,
      title: '买币',
      footer: 0
    }
  },
  {
    path: 'sell',
    component: () =>
      import( /* webpackChunkName: "my" */ '../views/sell'),
    meta: {
      requireLogin: true,
      title: '卖币',
      footer: 1
    }
  },
  ]
},
{
  path: '/about',
  name: 'about',
  component: () =>
    import( /* webpackChunkName: "about" */ '../views/About.vue')
},
{
  path: '/login',
  name: 'login',
  component: () =>
    import( /* webpackChunkName: "login" */ '../views/login.vue')
},
{
  path: '/register',
  name: 'register',
  component: () =>
    import( /* webpackChunkName: "register" */ '../views/register.vue')
},
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  let isLogin = localStorage.getItem('token')
  if (!to.meta || !to.meta.requireLogin) {
    return next()
  }
  if (['/login', '/register'].includes(to.name) && !isLogin) {
    return next({ path: '/login' })
  }
  if (to.fullPath === '/my/bind') {
    return next()
  }
  const user = store.state.user
  if (user && user.mobile && user.realname && user.bankcards.length) {
    return next()
  }
  next({ path: '/my/bind' })
})


export default router